﻿Imports System.IO
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices

Public Class RawPrinterHelper
	' Structure and API declarions:
	<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
	Structure DOCINFOW
		<MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String
		<MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String
		<MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String
	End Structure

	<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As Boolean
	End Function
	<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
	End Function
	<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
	End Function
	<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
	End Function
	<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
	End Function
	<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
	End Function
	<DllImport("winspool.Drv", EntryPoint:="WritePrinter", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)>
	Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
	End Function

	Public Shared Function SendBytesToPrinter(ByVal Printer As String, ByVal Bytes As IntPtr, ByVal Count As Int32) As Boolean
		Dim PrinterHandle As IntPtr
		Dim DocInfo As DOCINFOW = Nothing		  ' Describes your document (name, port, data type).
		Dim Written As Int32		 ' The number of bytes written by WritePrinter().
		Dim Success As Boolean
		'Set up the DOCINFO structure.
		DocInfo.pDocName = "Makbuz"
		DocInfo.pDataType = "RAW"
		If OpenPrinter(Printer, PrinterHandle, 0) Then
			If StartDocPrinter(PrinterHandle, 1, DocInfo) Then
				If StartPagePrinter(PrinterHandle) Then
					Success = WritePrinter(PrinterHandle, Bytes, Count, Written)
					EndPagePrinter(PrinterHandle)
				End If
				EndDocPrinter(PrinterHandle)
			End If
			ClosePrinter(PrinterHandle)
		End If
		Return Success
	End Function

	Public Shared Sub SendStringToPrinter(ByVal Printer As String, ByVal Text As String)
		Dim Bytes As IntPtr
		Dim Count As Int32
		Count = Text.Length()
		Bytes = Marshal.StringToCoTaskMemAnsi(Text)
		SendBytesToPrinter(Printer, Bytes, Count)
		Marshal.FreeCoTaskMem(Bytes)
	End Sub

End Class